package com.yjd.bi.etl.user.service;

import com.yjd.bi.etl.comm.BaseService;
import com.yjd.bi.etl.comm.Constants;
import com.yjd.bi.etl.sql.UserSqlConstants;
import com.yjd.bi.report.model.BiDatabaseInfoModel;
import com.yjd.bi.report.util.BiUtil;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.Calendar;

/**
 * 存管数据统计
 * Created by yangchangyan on 2017/5/17.
 */
@Component("userPlatformDepoDataStatisticService")
public class UserPlatformDepoDataStatisticService extends BaseService {

    public void statistic() throws Exception {
        BiDatabaseInfoModel model = this.getBiDatabaseInfoModel();
        JdbcTemplate jt = BiUtil.getJdbcTemplate(model);
        long maxDate = 0l;

        // jt.update(UserSqlConstants.DELETE_DEPO_STATISTIC);
        maxDate = jt.queryForInt(UserSqlConstants.MAX_STASTIC_DATA);

        if (maxDate == 0) {
            // 没有统计过，则从存管上线前统计（2017/05/01/ 00:00:01）
            maxDate = 1493568001;
        } else {
            maxDate = DateUtil.dateStringToTimestamp(maxDate + "", DateUtil.PATTERN_yyyyMMdd) + Constants.DAYOFSECOND;
        }

        // 获取当天的0点的时间戳
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        long todayTime = todayStart.getTime().getTime() / 1000L;

        while (maxDate < todayTime) {
            // 统计前一天的用户数据
            statisticData(maxDate, jt);
            maxDate += Constants.DAYOFSECOND;// 加一天和当前时间比较，如果小于当前时间则继续统计

        }
    }

    private void statisticData(long maxDate, JdbcTemplate jt) {
        long beginDate = maxDate;
        long endDate = beginDate + Constants.DAYOFSECOND - 1;
        Long staticsDate = Long.parseLong(DateUtil.timestampToDateString(
                beginDate, DateUtil.PATTERN_yyyyMMdd));

        // 存管白名单人数
        int depo_whitelist_account = jt.queryForObject(UserSqlConstants.DEPO_WHITELIST_COUNT, new Object[]{beginDate, endDate}, Integer.class);

        // 开通存管人数
        int depo_account = jt.queryForObject(UserSqlConstants.DEPO_COUNT, new Object[]{beginDate, endDate}, Integer.class);

        // 开通存管后首次充值人数
        int first_recharge = jt.queryForObject(UserSqlConstants.FIRST_CHARGE, new Object[]{beginDate, endDate}, Integer.class);

        // 开通存管后首次投资人数
        int first_invest = jt.queryForObject(UserSqlConstants.FIRST_INVEST, new Object[]{beginDate, endDate}, Integer.class);

        // 开通存管后首次提现人数
        int first_cashOut = jt.queryForObject(UserSqlConstants.FIRST_CASHOUT, new Object[]{beginDate, endDate}, Integer.class);

        // 开通存管后充值金额
        double recharge_price = jt.queryForObject(UserSqlConstants.CHARGE_PRICE, new Object[]{beginDate, endDate}, Double.class);

        // 开通存管后投资金额
        double invest_price = jt.queryForObject(UserSqlConstants.INVEST_PRICE, new Object[]{beginDate, endDate}, Double.class);

        // 开通存管后提现金额
        double cashOut_price = jt.queryForObject(UserSqlConstants.CASHOUT_PRICE, new Object[]{beginDate, endDate}, Double.class);

        // 开通存管后绑卡人数
        int depo_bind_card = jt.queryForObject(UserSqlConstants.DEPO_BIND_CARD, new Object[]{beginDate, endDate}, Integer.class);

        try {
            jt.update(UserSqlConstants.INSERT_DOPO_STASTIC_DATA,
                    staticsDate, depo_whitelist_account, depo_account, depo_bind_card, first_recharge, first_invest,
                    first_cashOut, recharge_price, invest_price, cashOut_price, FrameUtil.getTime());
        } catch (Exception e) {
            logger.info("批量处理插入失败，处理sql:" + UserSqlConstants.INSERT_DOPO_STASTIC_DATA + "错误信息:" + e);
        }
    }
}
